﻿@using StackExchange.Opserver.Data.HAProxy
@model StackExchange.Opserver.Views.HAProxy.HAProxyModel
@{
    this.SetMainTab(MainTab.HAProxy);
    Layout = "HAProxy.cshtml";
    
    var proxies = Model.Proxies;
}
@if (proxies.Any())
{
    var showRequestErrors = proxies.Any(p => p.Servers.Any(s => s.RequestErrors > 0));
    var showConnectionErrors = proxies.Any(p => p.Servers.Any(s => s.ConnectionErrors > 0));
    var showResponseErrors = proxies.Any(p => p.Servers.Any(s => s.ResponseErrors > 0));
    var errorCols = (showRequestErrors ? 1 : 0) + (showConnectionErrors ? 1 : 0) + (showResponseErrors ? 1 : 0);
<div class="proxies-wrap">
    <table class="proxies slim">
        @foreach (var p in proxies)
        {
            var colCount = 14;
            if (p.ShowQueue) { colCount += 3; }
            if (p.ShowWarnings) { colCount += 1; }
            if (p.ShowServers) { colCount += 6; }
            <tbody class="top-header">
                <tr>
                    <td colspan="@(colCount)"><h2 title="@p.Name">@p.Instance.Group.Name @p.Instance.Name: @p.NiceName</h2></td>
                </tr>
                <tr class="main-row-header">
                    <th rowspan="2" class="top-left"></th>
                    @if (p.ShowQueue)
                    {
                        <th colspan="2">Queue</th>
                    }
                    <th @((p.ShowQueue ? "colspan=\"3\"" : "colspan=\"2\"").AsHtml())>Session Rate</th>
                    <th colspan="4">Sessions</th>
                    <th colspan="2">Bytes</th>
                    @*<th colspan="2">Denied</th>*@
                    @if (showRequestErrors || showConnectionErrors || showResponseErrors)
                    {
                        <th colspan="@(errorCols)">Errors</th>
                    }
                    @if (p.ShowWarnings)
                    {
                        <th>Warnings</th>
                    }
                    <th @((p.ShowServers ? (p.ShowThrottle ? "colspan=\"9\"" : "colspan=\"8\"") : "").AsHtml())>Server</th>
                </tr>
                <tr class="sub-row-header">
                    @if (p.ShowQueue)
                    {
                        <th title="Current Sessions in Queue">Cur</th>
                        <th title="Max Simultaneous Sessions in Queue">Max</th>
                        @*<th title="Limit of Sessions in Queue">Limit</th>*@
                    }
                    @* Session Rate *@
                    <th title="Current Sessions in Flight (per second)">Cur</th>
                    <th title="Max Simultaneous Sessions in Flight (per second)">Max</th>
                    @if (p.ShowQueue)
                    { 
                        <th title="Limit of Session in Flight (before they start queueing) (per second)">Limit</th>
                    }
                    @* Sessions *@                    
                    <th title="Current Sessions in Flight">Cur</th>
                    <th title="Max Simultaneous Sessions in Flight">Max</th>
                    @*<th title="Limit of Session in Flight (before they start queueing)">Limit</th>*@
                    <th title="Total Session Count">Total</th>
                    <th title="Total Load Balanced Sessions (times a server was selected)">LbTot</th>
                    @* Bytes *@
                    <th title="Total Bytes In">In</th>
                    <th title="Total Bytes In">Bytes Out</th>
                    @* Denied *@
                    @*<th title="Total Denied Requests">Req</th>*@
                    @*<th title="Total Denied Responses">Resp</th>*@
                    @* Errors *@
                    @if (showRequestErrors)
                    {
                        <th title="Total Request Errors">Req</th>
                    }
                    @if (showConnectionErrors)
                    {
                        <th title="Total Connection Errors">Conn</th>
                    }
                    @if (showResponseErrors)
                    {
                        <th title="Total Response Errors">Resp</th>
                    }
                    @* Warnings *@
                    @if (p.ShowWarnings)
                    {
                        <th title="Total Retries">Retr</th>
                        @*<th title="Total Redis">Redis</th>*@
                    }
                    @* Server Columns *@
                    <th title="The last time the status changed">Status</th>
                    @if (p.ShowServers)
                    {
                        <th title="Last Check Result">LastChk</th>
                        @*<th title="Server Weight">Weight</th>*@
                        @*<th title="Is this an active server?">Active</th>*@
                        @*<th title="Is this a backup server?">Backup</th>*@
                        <th title="Failed Health Checks">Chk</th>
                        <th title="How many times this server went down">Down</th>
                        <th title="Total time this server has been down">Downtime</th>
                        if (p.ShowThrottle)
                        {
                            <th title="Warm up Status">Throttle</th>
                        }
                    }
                </tr>
            </tbody>
            <tbody>
                @foreach (var s in p.AllStats)
                {
                    <tr class="proxy-@s.Type.ToString() status @Model.GetClass(s)">
                        <td class="name">@s.IconSpan() @(s.Type == StatusType.Server ? s.ServerName : s.Type.ToString())</td>
                        @if (p.ShowQueue)
                        {
                            <td>@s.CurrentQueue.ToComma()</td>
                            <td>@s.MaxQueue.ToComma()</td>
                            <td>@s.LimitQueue.ToComma()</td>
                        }
                        <td>@s.CurrentNewSessionsPerSecond.ToComma()</td>
                        <td>@s.MaxNewSessionPerSecond.ToComma()</td>
                        @if (p.ShowQueue)
                        { 
                            <td>@s.LimitNewSessionPerSecond.ToComma()</td>
                        }
                        <td>@s.CurrentSessions.ToComma("-")</td>
                        <td>@s.MaxSessions.ToComma()</td>
                        @*<td>@s.LimitSessions.ToComma("-")</td>*@
                        <td>@s.TotalSessions.ToComma("-")</td>
                        <td>@s.LoadBalanceTotal.ToComma("-")</td>

                        <td title="@s.BytesIn.ToComma() bytes">@s.BytesIn.ToHumanReadableSize()</td>
                        <td title="@s.BytesOut.ToComma() bytes">@s.BytesOut.ToHumanReadableSize()</td>

                        @*<td>@s.DeniedRequests.ToComma("-")</td>*@
                        @*<td>@s.DeniedResponses.ToComma("-")</td>*@
                        
                        @if (showRequestErrors)
                        {
                        <td>@s.RequestErrors.ToComma("-")</td>
                        }
                        @if (showConnectionErrors)
                        {
                        <td>@s.ConnectionErrors.ToComma("-")</td>
                        }
                        @if (showResponseErrors)
                        {
                        <td>@s.ResponseErrors.ToComma("-")</td>
                        }
                    
                        @if (p.ShowWarnings)
                        {
                            <td>@s.WarningRetries.ToComma("-")</td>
                            @*<td>@s.WarningRedisPatches.ToComma("-")</td>*@
                        }
                        <td>@(s.Status != "no check" ? s.LastStatusChange.ToRelativeTimeMiniAll() : "") @(s.Status != "UP" ? " (" + s.Status + ")" : "")</td>
                    
                        @if (p.ShowServers)
                        {
                            <td title="@s.CheckStatusString - @s.CheckCode (@(s.CheckDurationMiliseconds)ms)">@s.CheckStatusString@(s.CheckCode > 0 && s.CheckCode != 200 ? " (" + s.CheckCode  + ")" : "")</td>
                            @*<td>@s.Weight</td>*@
                            @*if (s.IsServer)
                            { 
                                <td>@s.Active.ToYesNo()</td>
                                <td>@s.Backup.ToYesNo()</td>
                            }
                            else if (s.IsBackend)
                            { 
                                <td>@s.Active</td>
                                <td>@s.Backup</td>
                            }
                            else
                            { 
                                <td></td>
                                <td></td>
                            }*@
                            <td>@s.CheckFails.ToComma()</td>
                            <td>@s.UpDownTransitions.ToComma()</td>
                            <td>@s.DowntimeSeconds.ToTimeString()</td>
                            if (p.ShowThrottle)
                            {
                                <td>@s.Throttle.ToComma("")</td>
                            }
                        }
                    </tr>                
                }
            </tbody>
        }
    </table>
</div>
}